//
//  WelComeViewController.swift
//  sina
//
//  Created by DXapple on 15/11/14.
//  Copyright © 2015年 sqlite. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelComeViewController: UIViewController {
    
    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
       statrAnimation()
    }
    //MARK: 动画
    
    private func statrAnimation() {
    //从外面改变约束
        icoImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 180)
        }
        //动画效果  有缘基金  涉及到加速度
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //闭包内部强制刷新页面
            //讲提前收集到的约束的变化进行更新
            self.view.layoutIfNeeded()
            }) { (_ ) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_ ) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                        
                        })}}
    
    
    //MARK: 添加子视图
    private func setupUI() {
        view.addSubview(icoImage)
        view.addSubview(welcomeLabel)
        //设置布局
        icoImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(icoImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        icoImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage:UIImage(named: "avatar_default_big") )
        //圆角
        icoImage.layer.cornerRadius = 45
        //一定要裁剪不然可能没有删掉遮盖
        icoImage.layer.masksToBounds = true
        
        
    
    }
    
    
    
  //懒加载所有视图控件
    
    
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var icoImage: UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = UILabel(title: (UserAccountViewModel().account?.name ?? "") + " 今天约吗", size: 19, color: UIColor.lightGrayColor())
 
//    private lazy var welcomeLabel: UILabel = {
//        let l  = UILabel()
//            l.textColor = UIColor.lightGrayColor()
//            l.font = UIFont.systemFontOfSize(19)
//            l.textAlignment = .Center
//            l.text = (UserAccountViewModel().account?.name ?? "") + " 今天约吗"
//            l.alpha = 0
//            l.sizeToFit()
//        return l
//    }()
    
}



